<html>
<head><meta charset="utf-8"><title>Async Lifecycles · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html">Async Lifecycles</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="185737833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185737833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185737833">(Jan 15 2020 at 20:00)</a>:</h4>
<p>(Quick notes: <span class="user-mention" data-user-id="116009">@nikomatsakis</span> proposed something like this and I fleshed out portions of it with <span class="user-mention" data-user-id="116883">@tmandry</span>. Eliza Weisman helped me flesh this out.)</p>
<p>I wrote up a rough proposal for a <code>Lifecycle</code> trait, which is meant to support certain types of RAII guards in asynchronous contexts. Specifically, it exposes methods for a type to be notified if was yielded/resumed in a future or a generator. I have a draft which isn't complete, but is shareable: <a href="https://paper.dropbox.com/doc/Lifecycle-Trait--Asc4LYWRnMAQVIWxKRCNDP_3Ag-xW4HXDFuNGjeGdq9RkDAF" target="_blank" title="https://paper.dropbox.com/doc/Lifecycle-Trait--Asc4LYWRnMAQVIWxKRCNDP_3Ag-xW4HXDFuNGjeGdq9RkDAF">https://paper.dropbox.com/doc/Lifecycle-Trait--Asc4LYWRnMAQVIWxKRCNDP_3Ag-xW4HXDFuNGjeGdq9RkDAF</a>.</p>



<a name="185738122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185738122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185738122">(Jan 15 2020 at 20:03)</a>:</h4>
<p>I wanted to share this here before I posted a more fleshed-out proposal on internals.</p>



<a name="185854180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854180">(Jan 16 2020 at 20:07)</a>:</h4>
<p><span class="user-mention" data-user-id="198819">@David Barsky</span> :</p>
<blockquote>
<p>It would be implemented as a fundamental trait, like Drop. This implies a blanket implementation for all types. </p>
</blockquote>
<p>Note that <code>Drop</code> is <em>not</em> implemented for all types. In fact, the <code>pin-project</code> macro relies on this fact in order to to prevent user-written <code>Drop</code> impls of certain annotated types.</p>
<p>Instead, the compiler has the notion of "drop glue", where types that are <code>!Drop</code> (e.g <code>String</code>) nevertheless "do something" when dropped (invoking drop glue for fields)</p>
<p>If <code>Lifecycle</code> is impemented for all types (e.g. <code>T: Lifecycle</code> always holds), then this would need to rely on specialization in some way</p>



<a name="185854379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854379">(Jan 16 2020 at 20:09)</a>:</h4>
<p>If we <code>Lifecycle</code> to work in the same way as as <code>Drop</code>, we we need to introduce a notion of <code>Lifecycle</code> glue. This would require making <code>Lifecycle</code> even more deeply integrated into the core language</p>



<a name="185854386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854386">(Jan 16 2020 at 20:10)</a>:</h4>
<p><span class="user-mention" data-user-id="125294">@Aaron Hill</span> oh, that's _really_ cool—I didn't know that. in that case, <code>Lifecycle</code> should follow Drop's footsteps, or it is a "magic" trait like <code>Deref</code> which is implemented on only _some_ types.</p>



<a name="185854505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854505">(Jan 16 2020 at 20:10)</a>:</h4>
<p>Did you mean <code>Drop</code> instead of <code>Deref</code>?</p>



<a name="185854531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854531">(Jan 16 2020 at 20:11)</a>:</h4>
<p>that was a confusingly worded sentence :)</p>



<a name="185854560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854560">(Jan 16 2020 at 20:11)</a>:</h4>
<p>AFAIK, <code>Deref</code> is only magical in that it has syntactic sugar, and can be automatically <em>invoked</em></p>



<a name="185854588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854588">(Jan 16 2020 at 20:11)</a>:</h4>
<p>what i'm <em>trying</em> to get to is that this trait should be <em>minimal</em> and opt-in.</p>



<a name="185854597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854597">(Jan 16 2020 at 20:11)</a>:</h4>
<p>But "<code>Deref</code> glue" doesn't exist</p>



<a name="185854654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854654">(Jan 16 2020 at 20:12)</a>:</h4>
<p>and ideally, would reduce how much work is needed to support to it.</p>



<a name="185854705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854705">(Jan 16 2020 at 20:12)</a>:</h4>
<p>ideally, <code>Lifecycle</code> shouldn't rely on specialization or negative bounds.</p>



<a name="185854807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185854807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185854807">(Jan 16 2020 at 20:13)</a>:</h4>
<p>it's meant to be more of a hint to the compiler along the lines of: "if you see a type implementing a trait named <code>Lifecycle</code>, do this codegen"</p>



<a name="185855456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185855456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185855456">(Jan 16 2020 at 20:19)</a>:</h4>
<p>On the design level, I'm concerned about how implicit this is. From the perspective of an <code>async fn</code> author, arbitrary code is being injected both before and after every <code>.yield</code>.</p>
<p>With <code>Drop</code>, you can look at the indentation of a variable to get a hint as to where its drop glue will run (assuming it's not moved out of). With <code>Lifecycle</code>, you essentially need to keep a mental stack of all locals, which you consult whenever you see a <code>.yield</code></p>



<a name="185855838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185855838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185855838">(Jan 16 2020 at 20:22)</a>:</h4>
<p>I think the explicit approach of annotating the function makes things a lot more readable. I think composing futures is much easier to understand (analogous to the composition of functions) in terms of existing concepts. The <code>Lifecycle</code> trait is in many ways a "new" concept that all users need to be aware of in order to understand the behavior of their code</p>



<a name="185857733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/185857733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#185857733">(Jan 16 2020 at 20:42)</a>:</h4>
<p>Thanks for the design feedback—the "implicitness" is a concern for me as well, and I'm not 100% sold on the utility of this. I'd like to fully understand if some sort of task locals/structured concurrency <em>could</em> address the scoping of spans without language changes.</p>



<a name="187009854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/187009854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#187009854">(Jan 30 2020 at 17:30)</a>:</h4>
<p>I realized that Node's Async Hooks is some prior work similar to this proposal: <a href="https://nodejs.org/api/async_hooks.html" target="_blank" title="https://nodejs.org/api/async_hooks.html">https://nodejs.org/api/async_hooks.html</a></p>



<a name="197016831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Async%20Lifecycles/near/197016831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Async.20Lifecycles.html#197016831">(May 09 2020 at 22:21)</a>:</h4>
<p>I posted on internals: <a href="https://internals.rust-lang.org/t/pre-rfc-lifecycle-trait/12311" title="https://internals.rust-lang.org/t/pre-rfc-lifecycle-trait/12311">https://internals.rust-lang.org/t/pre-rfc-lifecycle-trait/12311</a>. I don't think I fully grasped what <span class="user-mention" data-user-id="125294">@Aaron Hill</span> was saying until <span class="user-mention" data-user-id="270901">@Nika Layzell</span> pointed out a few big issues. I'm using that post primarily a "here's my motivation for proposing this; implementation TBD".</p>
<p>I think any sort of implementation around this would require some compiler magic, but I'd rather not cover them now because they're a bit of distraction to problem I'd like to address. I don't want to introduce an idée fixe by accident!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>